<?php
function a290_showresult($key) {
	global $REX;
	$sql = new rex_sql;
	$query = 'SELECT * FROM '.$REX['TABLE_PREFIX'].'290_haendler WHERE id='.$key;
	$sql->setQuery($query);
	if ($sql->getRows()>0) {
		echo '<div class="a290_detail">';
		if ($sql->getValue('haendler_logo')!="") {
			echo '<p class="logo"><img src="'.$REX['HTDOCS_PATH'].'/files/'.$sql->getValue('haendler_logo').'" /></p>';
		}
		
		echo '<div class="a290_kontaktdaten">';
		echo $sql->getValue('haendler_name');
		if ($sql->getValue('haendler_name_zusatz')!="")
		{	echo ' ('.$sql->getValue('haendler_name_zusatz').')';
		}
		echo '<br />';
		echo $sql->getValue('haendler_abteilung');
		echo '<br />';
		
		if ($sql->getValue('haendler_postfach')!="") {
			echo 'Postfach: ';
			echo $sql->getValue('haendler_postfach').'<br />';
		}
		
		$adresse = $sql->getValue('haendler_strasse')."\n".$sql->getValue('haendler_plz')." ";
		$adresse .= $sql->getValue('haendler_stadt')."\n".$sql->getValue('haendler_land');
		$adresse = explode("\n",$adresse);
		
		for ($j=0;$j<count($adresse);$j++)
		{	echo trim($adresse[$j]);	
			if ($j<count($adresse))
			{	echo '<br />';
			}
		}
		echo '<br />';
		if ($sql->getValue('haendler_telefon')!="")
		{	echo '<h3>Tel:</h3>'.$sql->getValue('haendler_telefon');
		}
		if ($sql->getValue('haendler_fax')!="")
		{	echo '<h3>Fax:</h3>'.$sql->getValue('haendler_fax');
		}
		if ($sql->getValue('haendler_email')!="")
		{	echo '<h3>E-Mail:</h3>'.'<a href="mailto:'.$sql->getValue('haendler_email').'">'.$sql->getValue('haendler_email').'</a>';
		}
		if ($sql->getValue('haendler_homepage')!="")
		{	echo '<h3>WWW:</h3>'.'<a href="'.$sql->getValue('haendler_homepage').'">'.$sql->getValue('haendler_homepage').'</a>';
		}
		$tex = new Textile;
		echo $tex->TextileThis($sql->getValue('haendler_sonstiges'));
		
		echo '</div>';
			
		echo '</div>';
	}
}
	
	echo '<form action="index.php" method="get">';
	$select = new rex_select;
	$select->setAttribute('name','a290_query');
	$select->addOption('---Alle Gebiete---','');
	if (isset($_GET['a290_query'])) {
		$select->setSelected($_GET['a290_query']);
	}
	$select->addSqlOptions('SELECT haendler_gebiet as label,id FROM '.$REX['TABLE_PREFIX'].'290_gebiete ORDER BY haendler_gebiet ASC');
	$select->setSize(1);
	$select->show();
	echo '<input type="hidden" name="article_id" value="'.$this->article_id.'" />';
	echo '<input type="hidden" name="clang" value="'.$this->clang.'" />';
	echo '<input type="submit" name="a290_search" value="Anzeigen" />';
	echo '</form>';
	
	
	if (!isset($_GET['a290_showdetail'])) {
		$key = rex_request('a290_query','int');		
				
		$sql = new rex_sql;
		$query = 'SELECT * FROM '.$REX['TABLE_PREFIX'].'290_haendler WHERE haendler_status=1 ORDER BY haendler_name ASC';
		$sql->setQuery($query);
		
		$results = array();
		$names = $sql->getFieldnames();
		
		# filter
		for ($i=0;$i<$sql->getRows();$i++) {
			$tmp = explode('|+|',$sql->getValue('r_haendler_gebiete'));
			
			if (in_array($key,$tmp) || $key == 0 ) {
				$result = array();
				foreach ($names as $k=>$v) {
					$result[$v]=$sql->getValue($v);
				}
				$results[]=$result;
			}
			$sql->next();
		}
		
		
		# show results
		if (isset($results)) {
			if (count($results)>0) {
				echo '<div class="a290_results">';
				foreach ($results as $result) {
					
					if (rex_request('a290_query')=="") {
						if ($result['haendler_logo']!="")  {
							$params = array(	'a290_showdetail'=>$result['id'],
												'a290_query'=> $key 
											);
							echo '<a href="'.rex_getUrl($this->article_id,$this->clang,$params).'">';
							echo '<img src="index.php?rex_resize=100h__'.$result['haendler_logo'].'" alt="" />';
							echo '</a>';
						}
					} else {
						a290_showresult($result['id']);
					}
					
					
				}
				echo '</div>';
			}
		}
		
	} //eoIf not Detail
	
	if (isset($_GET['a290_showdetail'])) {
		$key = rex_request('a290_showdetail','int');
		# output
		if (rex_request('a290_query','int')>0) {
		$params = array(	'a290_query'=> rex_request('a290_query','int') 
					   );
		} else {
			$params = array();
		}
		echo '<a href="'.rex_getUrl($this->article_id,$this->clang,$params).'">Zur&uuml;ck</a>';
		
		if ($key>0) {
				a290_showresult($key);
		}
	}
		
	

?>